subroutine input
use var
use type
use fix
implicit none
integer :: i

!--- read input file ---
  open(111,file='control',status='old')
  read(111,nml = indata)
  close(111)

! Initial parameter
  Sbulk    = S( ULdG )            ! Scalar Order parameter  
  Sinit    = S(2.8d0)
! Allocate variabel
  
  tnodes = dime(1)*dime(2)*dime(3)

  allocate (q(6,tnodes))
  allocate ( boundary(dime(1),dime(2),dime(3)), boundary0(dime(1),dime(2),dime(3)),bulk(dime(1),dime(2),dime(3)))
  if (system=="droplet".or.system=="cylinder") then
     allocate ( drop(dime(1),dime(2),dime(3)))
     drop = .false.
  endif
  if (number_NP>0) then
     allocate ( NP(dime(1),dime(2),dime(3)))
     allocate (NPnodes(number_NP),NPbnodes(number_NP))
     NP = .false.
  endif
  allocate(boundary_NP(dime(1),dime(2),dime(3)))
  boundary_NP = .false.
  allocate (neighbors(6,tnodes))
  allocate (NP_position(3,number_NP))
  NP_position = 0
  allocate (trj_E(5,int(traj(1)/sample)))
  allocate(lb2index(tnodes)) ! conver label to index
  boundary = .false.; boundary0 = .false.; bulk = .false.
  
  channel=.false.;droplet=.false.;cylinder=.false.
  if (system=="channel") then
     channel=.true.
  elseif (system=="droplet") then
     droplet=.true.
  elseif (system=="cylinder") then
     cylinder=.true.
  else 
     Print *, "ERORR"
     stop
  endif

  infinite=.false.;nondegenerate=.false.;degenerate=.false.
  if (anchor=="inf") then
     infinite=.true.
  elseif (anchor=="nond") then
     nondegenerate=.true.
  elseif (anchor=="deg") then
     degenerate=.true.
  else
     Print *, "ERORR"
     stop
  endif

  NP_infinite=.false.;NP_nondegenerate=.false.;NP_degenerate=.false.
  if (NPanchor=="inf") then
     NP_infinite=.true.
  elseif (NPanchor=="nond") then
     NP_nondegenerate=.true.
  elseif (NPanchor=="deg") then
     NP_degenerate=.true.
  else
     Print *, "ERORR"
     stop
  endif

  write(*,nml = indata)
  write(222,nml = indata)
  write(*,*)"Sbulk    = ", Sbulk
  write(222,*)"Sbulk    = ", Sbulk
  write(*,*)"Sinitial = ", Sinit
  write(222,*)"Sinitial = ", Sinit

! RANDOM SEED
  call RANDOM_SEED
  call RANDOM_SEED(put = iseed(1:2))
  
contains
   Function S(u)
	  use type
	  implicit none
	  real(dp), intent(in):: u
	  real(dp) S
	  S = 0.25d0 + 0.75*sqrt( 1.d0 - 8.d0/(3.d0*U))
   end function S
end subroutine input 
